package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.keyBoard.*;
    import com.mui.controls.*;
    import flash.events.*;
    import modules.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.autoFight.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.game.view.revival.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ReliveController extends Controller
    {
        private var _revivalPropWindow:RevivalPropWindow;
        private var _sEntityKillerInfo:SEntityKillerInfo;

        public function ReliveController()
        {
            
        }

        public function get revivalPropWindow() : RevivalPropWindow
        {
            if (this._revivalPropWindow == null)
            {
                this._revivalPropWindow = new RevivalPropWindow();
            }
            return this._revivalPropWindow;
        }

        public function stageResize() : void
        {
            if (_view && !_view.isHide)
            {
                (_view as ReliveModule).stageResize();
            }
            
        }

        private function onCoinBindRelive(event:DataEvent) : void
        {
            if (RolePlayer.instance.isDead)
            {
                GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInSitu, EPrictUnit.EPriceUnitGoldBind);
            }
            else
            {
                view.hide();
            }
            
        }

        private function onCoinRelive(event:DataEvent) : void
        {
            if (RolePlayer.instance.isDead)
            {
                GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInSitu, EPrictUnit.EPriceUnitGold);
            }
            else
            {
                view.hide();
            }
            
        }

        private function onLocalRelive(event:DataEvent) : void
        {
            if (RolePlayer.instance.isDead)
            {
                GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInSitu, EPrictUnit.EPriceUnitGold);
            }
            else
            {
                view.hide();
            }
            
        }

        private function onPropLocal(event:DataEvent) : void
        {
            if (RolePlayer.instance.isDead)
            {
                GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInSitu, EPrictUnit.EPriceUnitGold);
            }
            else
            {
                view.hide();
            }
            
        }

        private function onCityRelive(event:DataEvent) : void
        {
            if (RolePlayer.instance.isDead)
            {
                GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInBackToTheCity, EPrictUnit.EPriceUnitGold);
            }
            else
            {
                view.hide();
            }
            
        }

        private function onAdd(event:Event) : void
        {
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            view.update(_loc_2, this._sEntityKillerInfo);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new ReliveModule();
            _loc_1.addEventListener(Event.ADDED_TO_STAGE, this.onAdd, false, 0, true);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            super.initServer();
            RolePlayer.instance.addEventListener(PlayerEvent.ENTITY_Relived, this.onRoleRelived, false, 0, true);
            NetDispatcher.addCmdListener(ServerCommand.Copy_Fail, this.onCopyFailHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillByEntity, this.onRoleKillByEntity);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateRevivalPropNotEnough, this.onRevivalPropNotEnoughHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_CancelBuyProp, this.onCancelBuyPropHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_SureBuyProp, this.onSureBuyPropHandler);
            Dispatcher.addEventListener(EventName.Role_Relive_City, this.onCityRelive);
            Dispatcher.addEventListener(EventName.Role_Relive_Local, this.onLocalRelive);
            Dispatcher.addEventListener(EventName.Role_Relive_PropLocal, this.onPropLocal);
            
        }

        private function onRoleRelived(event:PlayerEvent) : void
        {
            view.hide();
            this.revivalPropWindow.hide();
            KeyBoardManager.instance.addListener();
            Game.scene.mouseEnabled = true;
            if (AutoFight.currentAutoFight.isAutoFight)
            {
                AIManager.onAIControl(null, AIType.AI_AutoFight);
            }
            
        }

        private function onRoleDead(event:PlayerEvent) : void
        {
            this.roleDead();
            RolePlayer.instance.removeEventListener(PlayerEvent.ENTITY_DEAD, this.onRoleDead, false);
            
        }

        private function onRoleKillByEntity(param1:MessageBlock) : void
        {
            this._sEntityKillerInfo = param1.messageBase as SEntityKillerInfo;
            this.roleDead();
            
        }

        private function roleDead() : void
        {
            KeyBoardManager.instance.cancelListener();
            Game.scene.mouseEnabled = false;
            if (Game.sceneInfo.isCanRevival == false)
            {
                
            }
            if (GameMapUtil.isCopyMap(MapFileUtil.mapID))
            {
                if (Game.sceneInfo.sMapDefine.restrictionType & EMapRestrictionType._EMapRestrictionTypeRevivalInSitu && Game.sceneInfo.sMapDefine.restrictionType & EMapRestrictionType._EMapRestrictionTypeRevivalInBackToTheCity)
                {
                    
                }
            }
            if (AutoFight.currentAutoFight.isAutoFight)
            {
                if (AutoFight.currentAutoFight.localeRevive)
                {
                    if (cache.login.loginGame.money.gold >= 10)
                    {
                        GameProxy.sceneProxy.revival(ERevivalType.ERevivalTypeInSitu, EPrictUnit.EPriceUnitGold);
                        MsgManager.addBroadCast("自动挂机原地复活");
                        
                    }
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFightEnd));
            }
            view.show();
            
        }

        private function onCopyFailHandler(event:DataEvent) : void
        {
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.timerOut = 10;
            Alert.buttonWidth = 60;
            Alert.show("你已经被击败，10秒后将会被传出副本", null, Alert.OK, null, this.onAlertOkHandler);
            
        }

        private function onAlertOkHandler(param1:int) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeaveWithoutComfirm));
            
        }

        private function onRevivalPropNotEnoughHandler(param1:MessageBlock) : void
        {
            this.revivalPropWindow.show();
            
        }

        private function onCancelBuyPropHandler(event:DataEvent) : void
        {
            this.revivalPropWindow.hide();
            view.show();
            
        }

        private function onSureBuyPropHandler(event:DataEvent) : void
        {
            var _loc_4:int = 0;
            var _loc_6:ShopItemInfo = null;
            var _loc_2:* = event.data as int;
            var _loc_3:* = GameDefConfig.instance.getEShopCode(ShopType.BindShop);
            var _loc_5:* = ShopConfig.instance.getShopItemInfoArrayByType(ECategory._ECategoryProp, EProp._EPropRevival);
            for each (_loc_6 in _loc_5)
            {
                
                if (_loc_6 != null && _loc_6.itemInfo.item.bind == EBind._EBindYes)
                {
                    _loc_4 = _loc_6.itemInfo.item.code;
                    break;
                }
            }
            GameProxy.shopProxy.buyItemAndUse(0, _loc_3, _loc_4, _loc_2, 1);
            
        }

    }
}
